const log = t => getApp().globalData.logger.log('[featureSlider]' + t)

Component({
  mixins: [],
  data: {
    selected: {
      index: 0,
      type: '',
      value: 0
    }
  },
  props: {
    disabled: false,
    pickerOptions: [], // { type: '', name: '', min: 0, max: 100 }
    values: {}, // key: type, value: sliderValue
    onSliderSwitch: () => {},
    onSliderValueChange: () => {},
    onSliderValueChanging: () => {}
  },
  didMount() {},
  didUpdate(prevProps) {
    if (this.props.values !== prevProps.values) {
      const type = this.props.pickerOptions[this.data.selected.index].type
      this.setData({
        selected: {
          ...this.data.selected,
          type,
          value: this.props.values[type]
        }
      })
    }
  },
  didUnmount() {},
  methods: {
    onPickerChange: function(e) {
      const index = e.detail.value
      const type = this.props.pickerOptions[index] && this.props.pickerOptions[index].type
      const selected = {
        index,
        type,
        value: this.props.values[type]
      }
      this.setData({ selected })
      this.props.onSliderSwitch(selected)
    },
    onValueChange: function(e) {
      this.props.onSliderValueChange({
        type: this.data.selected.type,
        value: e.detail.value
      })
    },
    onValueChanging: function(e) {
      this.props.onSliderValueChanging({
        type: this.data.selected.type,
        value: e.detail.value
      })
    }
  },
});
